package com.neusoft.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.neusoft.entity.Room;
import com.neusoft.entity.common.CommonResult;
import com.neusoft.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;

@RestController
@RequestMapping("room")
public class RoomController {
    /**
     * 服务对象
     */
    @Resource
    @Autowired
    private RoomService roomService;

    @GetMapping("queryById/{id}")
    @ResponseBody
    public Object queryById(@PathVariable Integer id){
        System.out.println("查询单条数据"+id);
        Room bean = roomService.queryById(id);
        System.out.println("bean对象为："+bean);
        return bean;
    }

    @PostMapping("deleteById/{ids}")
    @ResponseBody
    public boolean deleteById(@PathVariable Integer[] ids) {
        if (ids == null || ids.length == 0) {
            return false;
        }
        System.out.println(ids);
        roomService.deleteById(Arrays.asList(ids));
        return true;
    }

    @PostMapping("delete1/{id}")
    @ResponseBody
    public boolean deleteById1(@PathVariable Integer id) {
        if ( id == null) {
            return false;
        }
        System.out.println(id);
        return roomService.deleteById1(id);
    }

    @RequestMapping("queryAll/{page}/{limit}")
    public Object queryAll(@PathVariable Integer page, @PathVariable Integer limit, @RequestBody Room bean) {
        System.out.println(bean+"jj");
        CommonResult<Room> result = new CommonResult<>();
        System.out.println(bean+"kk");
        IPage<Room> ipage = roomService.queryAllByLimit(page,limit,bean);
//        System.out.println(bean+"ll");
        result.setCode(0);
        result.setCount(ipage.getTotal());
        result.setData(ipage.getRecords());
        System.out.println(result);
        return result;
    }

    @PutMapping("save")
    @ResponseBody
    public boolean save(@RequestBody Room bean) {
        boolean result = false;
        if (bean.getId() != null) {
            result = roomService.update(bean) > 0;
        } else {
            result = roomService.insert(bean).getId() != null;
        }
        return result;
    }

}
